#!/usr/bin/python
# -*- coding: utf-8 -*-

########## DO NOT CHANGE THESE LINES ###########################################
import sys
from pylot.core import (ENCODING, Path)
try : __file__
except NameError : co = sys._getframe(0).f_code ; __file__ = co.co_filename
ufile = __file__.decode(ENCODING)
if ufile.strip() : PLUGIN_PATH = Path(ufile).abspath().parent
else : PLUGIN_PATH = Path(u'.').abspath()
########## DO NOT CHANGE THESE LINES ###########################################

from pylot.core import (ENCODING, Path, hardbook)
from pylot.interfaces import (HandlerInterface)

import libi18n

class HandlerPart (HandlerInterface):
  def __init__ (self, plugin_path=None, plugin_id=None):
    if plugin_path is None : plugin_path = PLUGIN_PATH
    HandlerInterface.__init__(self, plugin_path, plugin_id)
    # add main options, don't save it automatically
    self.addOptions(u'conf.csv', main=True)

  def _arrangePathLang(self, path=None, lang=None):
    if path is None :
      path = self.opt[u'lastpaths'](u'plugins')
    else :
      path = Path(path)
      if not path.isdir() :
        path = self.opt[u'lastpaths'](u'plugins')
    return path, self._lang(lang)

  def _lang(self, lang=None):
    if lang is None :
      lang = self.opt[u'translation'](u'lang')
    return lang

  def compileTranslation(self, plugin_path, lang=None):
    cg = libi18n.cg_compile_translation(plugin_path, self._lang(lang),
                                        tr_=self.tr_)
    return self.process.run(cg)

  def updateTranslation(self, plugin_path, lang=None, additional_paths=[],
                        ui_dirs=[]):
    cg = libi18n.cg_update_translation(plugin_path, self._lang(lang),
                                       additional_paths, ui_dirs, tr_=self.tr_)
    return self.process.run(cg)

  def initTranslation(self, plugin_path, lang=None, additional_paths=[]):
    u"""
    Warning: Use this method only the first time
    """
    cg = libi18n.cg_init_translation(plugin_path, self._lang(lang),
                                     tr_=self.tr_)
    return self.process.run(cg)

  def actionInitTranslation(self):
    self.initTranslation(*self._arrangePathLang())

  def actionUpdateTranslation(self):
    self.updateTranslation(*self._arrangePathLang())

  def actionCompileTranslation(self):
    self.compileTranslation(*self._arrangePathLang())

  def actionCompilepyLotTranslation(self):
    path = hardbook(u'pylotsrc', u'pylot')
    path, lang = self._arrangePathLang(path)
    cg = libi18n.cg_compile_translation(path, lang, tr_=self.tr_)
    return self.process.run(cg)

  def actionUpdatepyLotTranslation(self):
    path = hardbook(u'pylotsrc', u'pylot')
    path, lang = self._arrangePathLang(path)
    add_paths = [Path(path, u'_pylot')]
    ui_dirs = [Path(path, u'share', u'qtdesigner')]
    return self.updateTranslation(path, lang, add_paths, ui_dirs)

  def _writeSettings(self, settings, common):
    for book_id in self.opt :
      settings.beginGroup(u'tools_i18n_%s' % book_id)
      settings.setBook(self.opt[book_id])
      settings.endGroup()

  def _readSettings(self, settings, common):
    for book_id in self.opt :
      settings.beginGroup(u'tools_i18n_%s' % book_id)
      book = settings.getBook(default_book=self.opt[book_id])
      self.opt[book_id].userUpgrade(book)
      settings.endGroup()

if __name__ == '__main__':
  pass
